<?php
/**
 * Cube PHP Framework
 * 
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * @author Dillen / Steffen
 */

namespace Library;
 
/**
 * The application
 * 
 * @class Library.Application
 */
class Application
{
	/**
	 * Config instance
	 * 
	 * @property {Library.Application.Config} config
	 */
	private $_config;
	
	/**
	 * Holds the Bootstrap instance
	 * 
	 * @property {Library.Mvc.Bootstrap} _bootstrap
	 * @private
	 */
	private $_bootstrap;
	
	/**
	 * Constructor
	 *
	 * Create a new application
	 * 
	 * @method __construct
	 * @param {String} $module Module name
	 * @param {String} [$path=null] Module path
	 */
	public function __construct($settings)
	{
		$this->_config = new ArrayObject($settings);
	}
    
    public function getConfig()
    {
        return $this->_config;
    }
	
	/**
	 * Run application
	 * 
	 * @method run
	 */
	public function dispatch()
	{        
        $autoloader = \Library\Platform::getAutoloader();
		
		//Register application namespaces
		$autoloader->registerNamespace($this->_config['application']['namespace'] . '\\Controllers', $this->_config['application']['path']. '/controllers/');
		$autoloader->registerNamespace($this->_config['application']['namespace'] . '\\Models', $this->_config['application']['path']. '/models/');
		
        //Dispatch the bootstrapper
		if (file_exists($this->_config['application']['path'] . '/Bootstrap.php'))
		{
			include( $this->_config['application']['path'] . '/Bootstrap.php' );
			
			$class = '\\' . $this->_config['application']['namespace'] . '\\Bootstrap';
		}
		else 
		{
			$class = '\\Library\\Mvc\\Bootstrap';
		}
		
		$this->_bootstrap = new $class($this);
	}
}